/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-07 18:00:22
 */
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';
import { LoggerMiddleware } from './common/log4js/logger.middleware';
import { CategoryModule } from './modules/category/category.module';
import { AdminUserModule } from './modules/admin-user/admin-user.module';
import { RoleModule } from './modules/role/role.module';
import { UploadModule } from './modules/upload/upload.module';
import { ArticleModule } from './modules/article/article.module'
import { DefaultDataService } from './DefaultDataService.service';
import { WebConfigModule } from './modules/web-config/web-config.module';
import { AuthGuard } from './common/auth/auth.guard';
import { jwtConstants, signOptions } from './common/auth/constants';
import { JwtModule } from '@nestjs/jwt';
import configuration from '../config/configuration';
import { ConfigModule } from '@nestjs/config';
// 环境变量加载
const envFilePath = ['env/.env'];
if (process.env.NODE_ENV) {
  envFilePath.unshift(`env/.env.${process.env.NODE_ENV}`);
}
@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
      envFilePath,
    }),
    // 限度客户端在规定的时间内申请次数
    ThrottlerModule.forRoot({
      ttl: 60, //1分钟
      limit: 60, //申请10次
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.HOST,
      port: 3306,
      username: process.env.MYSQLNAME,
      password: process.env.MYSQLPASSWORD,
      database: process.env.MYSQLDATANASE,
      synchronize: true,
      cache: true,
      autoLoadEntities: true,
    }),
    CategoryModule,
    AdminUserModule,
    RoleModule,
    UploadModule,
    ArticleModule,
    WebConfigModule,
    JwtModule.register({
      secret: jwtConstants.secret,
      signOptions,
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    AdminUserModule,
    DefaultDataService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      .forRoutes({ path: 'article', method: RequestMethod.GET });
  }
}
